<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class FileEntry extends AppObj
{
	
	var $error;
	
	var $properties = array('id'		=> '',
							'filename'		=> '',
							'filesize'		=> '',
							'filetype'	 	=> '',
							'title' 		=> '',
							'description'	=> '',
							'description_full'	=> '',
							'filetext'		=> '',
							'comment'		=> '',
							'downloads'		=> 0,
							'date_posted'	=> '',
							'directory'		=> '',
							'sub_directory'	=> '',
							'author_id'		=> '',
							'updater_id'	=> '',
							'sort_order'	=> 0,
							'private'		=> 0,
							'active'		=> 1
							);
	
	//, 'author_id', 'updater_id', 'date_posted'
	var $hidden = array('id', 'author_id', 'updater_id', 
	                    'filename', 'filesize', 'filetype', 'downloads', 'date_posted', 'subdir');
	var $category = array();
	var $category_mirror = array();
	var $text;
	var $member_role = array();
	
	
	function setCategoryMirror($val) {
		$this->category_mirror = &$val;
	}
	
	function &getCategoryMirror() {
		return $this->category_mirror;
	}	
	
	function setCategory($val) {
		$this->category = &$val;
	}
	
	function &getCategory() {
		return $this->category;
	}
	
	function getMemberRole() {
		return $this->member_role;
	}
	
	function setMemberRole($role) {
		$this->member_role = $role;
	}	
	
	
	
	function _callBack($property, $val) {
	
		if($property == 'date_posted' && !$val) {
			$val = date('Y-m-d H:i:s');
		
		} elseif($property == 'author_id' && !$val) {
			$val = $_SESSION['member_']['user_id'];
		
		} elseif($property == 'updater_id') {
			$val = $_SESSION['member_']['user_id'];
		} 
		
		return $val;
	}
	
	
	function validate($values, $action) {
		
		require_once 'eleontev/Validator.php';
		
		$required = array('category');
		
		if($action == 'insert') {
			$required[] = 'file';
			$values['file'] = $_FILES['file_1']['name'];
		}
		
		if($action == 'bulkadd') {
			$values['directory'] = str_replace(array('/', '.', '..'), '', $values['directory']);
			$required[] = 'directory';
		}		
		
		$v =& new Validator($values, true);

		// check for required first, return errors
		$v->requiredArray('required_msg', $required);
		if($v->getErrors()) {
			$this->errors =& $v->getErrors();
			return true;
		}
		
		
		if($action == 'bulkadd') {
			if(!is_readable($values['directory'])) {
				$v->setError('dir_not_readable_msg', 'directory');
			}
		}
		
		if($v->getErrors()) {
			$this->errors =& $v->getErrors();
			return true;
		}
	}
}
?>